//
//  HMStatusViewModel.swift
//  weibo
//
//  Created by LCH on 16/3/10.
//  Copyright © 2016年 lch. All rights reserved.
//

import UIKit

class HMStatusViewModel: NSObject {
    
    var status:HMStatus? {
    
        didSet {
        
        print("status didset")
        
        }
       }
    
    // 当前发这条微博的用户的认证图标
    var verifiedImage: UIImage?
    // 会员图标
    var membershipImage: UIImage?
    
    // 转发数量
    var repostsCountStr: String?
    // 评论数量
    var commentsCountStr: String?
    // 点赞数量
    var attitudesCountStr: String?
    
    var retweetedText: String?
    
    //TODO:什么时候调用
    init(status:HMStatus) {
        
        super.init()
        self.status = status
        
        let verifiedType = status.user?.verified ?? -1
        switch verifiedType {
        case 1:
            verifiedImage = UIImage(named: "avatar_vip")
        case 2,3,5:// 企业认证
            verifiedImage = UIImage(named: "avatar_enterprise_vip")
        case 220: // 达人
            verifiedImage = UIImage(named: "avatar_grassroot")
        default:
            verifiedImage = nil
            
        
        }
        
        if let mbrank = status.user?.mbrank where mbrank > 0 {
        
        
        membershipImage = UIImage(named: "common_icon_membership_level\(mbrank)")
        
        }
        
        repostsCountStr = stringWithCount(status.reposts_count, defaultString: "转发")
        commentsCountStr = stringWithCount(status.comments_count, defaultString: "评论")
        attitudesCountStr = stringWithCount(status.attitudes_count, defaultString: "赞")
        
        
        if let screenName = status.retweeted_status?.user?.screen_name,text = status.retweeted_status?.text {
        
          retweetedText = "@\(screenName):\(text)"
        
        }else {
        
          retweetedText = "该微博已删除"
        }
    }
  
    private func stringWithCount(count:Int, defaultString:String) -> String {
    
        if count<10000 {
        
            if count == 0 {
            
              return defaultString
            }else {
            
             return "\(count)"
            }
        
        }else {
        
        let result = CGFloat(count/1000)/10
        let resultStr = "\(result)"
            if resultStr.containsString(".0"){
            
            return resultStr.stringByReplacingOccurrencesOfString("", withString: "")
            
            }
         return resultStr
        
        
        }
    
    
    
    
    }
    
}
